package taskTracker;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;

import taskTracker.util.Serializable;

/**
 * A task is the entity that can hold logged time.
 * @author aseldawy
 *
 */
public class Task extends TaggableObject implements Serializable {
	/**Name of this task*/
	private String name;
	/**Notes about this task*/
	private String notes;
	/**The list in which it resides*/
	Project parent;
	/**Its due date*/
	private long dueTo;
	/**Whether it can hold time entries or not*/
	private boolean trackable;
	
	public Task(String name, String notes) {
		this.name = name;
		this.notes = notes;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNotes() {
		return notes;
	}

	public void setNotes(String notes) {
		this.notes = notes;
	}

	public boolean isTrackable() {
		return trackable;
	}

	public void setTrackable(boolean trackable) {
		this.trackable = trackable;
	}

	public Project getParent() {
		return parent;
	}

	public long getDueTo() {
		return dueTo;
	}

	public void setDueTo(long dueTo) {
		this.dueTo = dueTo;
	}

	public Task(DataInputStream dis) {
		try
		{
			super.deserialize(dis);
			name=dis.readUTF();
			notes=dis.readUTF();
			dueTo= dis.readLong();
			trackable = dis.readBoolean();
			int timeEntriesSize = dis.readInt();
			for (int i=0;i<timeEntriesSize;i++)
			{
				TimeEntry timeEntry = new TimeEntry(dis);
				TimeEntries.getInstance().logTime(this, timeEntry.getFrom(), timeEntry.getTo(), timeEntry.getComment());
			}
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
	}

	public void serialize(DataOutputStream dos) {
		try
		{
			super.serialize(dos);
			dos.writeUTF(name);
			dos.writeUTF(notes);
			dos.writeLong(dueTo);
			dos.writeBoolean(trackable);	
			Vector timeEntries= TimeEntries.getInstance().getEntriesForTask(this);
			//write the size of this vector first
			dos.writeInt(timeEntries.size());
			for (int i=0;i<timeEntries.size();i++)
			{
				((TimeEntry)timeEntries.elementAt(i)).serialize(dos);
			}
		}
		catch(IOException e)
		{
			System.out.println(e);
		}
		
	}

	public String toString() {
		return this.getName();
	}

	public void removeTimeEnries() {
		// Delete its entries from logged time
		TimeEntries.getInstance().removeTimeEntriesFor(this);
	}

	public boolean isCompleted() {
		// TODO implement this method
		return false;
	}

	
}
